package DataConverters.DataReaders.readersImpl.defaultDataReader.Parser;

import DataConverters.exceptions.MissingNumberColumn;
import FileInformation.providers.DataSeparatorProvider;
import java.util.StringTokenizer;

/**
 * Created by IntelliJ IDEA.
 * User: Nobody
 * Date: 24.04.11
 * Time: 23:52
 * To change this template use File | Settings | File Templates.
 */
public class NumberParser {

    private DataSeparatorProvider dataSeparatorProvider;

    public NumberParser(DataSeparatorProvider dataSeparatorProvider) {
        this.dataSeparatorProvider = dataSeparatorProvider;
    }

    public double[] parseCharArrayToFloatArray(char[] line, int columnSize) throws MissingNumberColumn, NumberFormatException {
        String strLine = new String(line);
        StringTokenizer tokenizer = new StringTokenizer(strLine, dataSeparatorProvider.getChosenSeparatorValue());
        double[] tmp = new double[tokenizer.countTokens()];
        int i = -1;
        while (tokenizer.hasMoreTokens()) {
            tmp[++i] = Double.parseDouble(tokenizer.nextToken());
        }
        /* float[] tmp = new float[countNumbersPresentInCharArray(line)];
        int itemNr = -1;

        for (int i = 0, start = 0; i < line.length; ++i) {
        replaceSeparatorsOccurrenceInArray(line, i);
        if (Character.isDigit(line[i]) && (i + 1) == line.length ) {
        tmp[++itemNr] = parseCharArrayToFloat(line, start, i  - start);
        } else if (isAtTheEndOfNumber(line, i)) {
        tmp[++itemNr] = parseCharArrayToFloat(line, start, i + 1 - start);
        start = i + 1;
        }
        }

        
         */
        if (tmp.length != columnSize) {
            throw new MissingNumberColumn("Missing Column ." + "expected number of columns : " + columnSize + " acutal :" + tmp.length);
        }


        return tmp;
    }

    public double[] parseCharArrayToFloatArray(String line, int columnSize) throws MissingNumberColumn {
     
        String[] items  = line.split(dataSeparatorProvider.getChosenSeparatorValue());
        double[] tmp = new double[Math.min(columnSize, items.length)];
        int i = -1;
        for(String item : items){
            if(!item.equals(""))
            tmp[++i] = Double.parseDouble(item);
        }
     


        if (tmp.length != columnSize) {
            throw new MissingNumberColumn("Missing Column ." + "expected number of columns : " + columnSize + " acutal :" + tmp.length);
        }


        return tmp;
    }

    public int countNumbersPresentInCharArray(String line) {
        int itemNumber = 0;
        for (int i = 0; i < line.length() - 1; ++i) {
            if (Character.isDigit(line.charAt(i)) && (i + 1) == line.length() - 1) {
                ++itemNumber;
            } else if (isAtTheEndOfNumber(line.charAt(i), line.charAt(i + 1))) {
                ++itemNumber;
            }
        }
        return itemNumber;
    }

    public int countNumbersPresentInCharArray(char[] line) {
        int itemNumber = 0;
        for (int i = 0; i < line.length - 1; ++i) {
            if (Character.isDigit(line[i]) && (i + 1) == line.length - 1) {
                ++itemNumber;
            } else if (isAtTheEndOfNumber(line[i], line[i + 1])) {
                ++itemNumber;
            }
        }
        return itemNumber;
    }

    private boolean isAtTheEndOfNumber(char lastDigit, char whiteSpace) {
        return Character.isDigit(lastDigit) && (isSeparator(whiteSpace) || Character.isWhitespace(whiteSpace));
    }

    /*  private boolean isAtTheEndOfNumber(char[] line, int i) {
    if (i > line.length) {
    return false;
    }
    return Character.isDigit(line[i]) && (isSeparator(line[i + 1]) || Character.isWhitespace(line[i + 1]));
    }

    private float parseCharArrayToFloat(char[] buffer, int offset, int length) {
    return Float.valueOf(String.copyValueOf(buffer, offset, length));
    }

    private void replaceSeparatorsOccurrenceInArray(char[] buffer, int pos) {
    for (char c : separators) {
    if (buffer[pos] == c) {
    buffer[pos] = ' ';
    }
    if (buffer[pos] == c) {
    buffer[pos] = ' ';
    }
    }

    }*/
    private boolean isSeparator(Character c) {
        for (String s : dataSeparatorProvider.getSeparators().values()) {
            if (s == null ? c.toString() == null : s.equals(c.toString())) {
                return true;
            }
        }
        return false;
    }
}
